define(
	[
	],
	function(
	){
	var albumPhoto = {
		params:{
			url:"http://www.atphotos.cn/atphotos",
			laydate:null,
			userId:'',
			address:''
		},
		initPage:function(){
		   	albumPhoto.showUserAlbum('1','10') // 获得相册数据
		    albumPhoto.repairAlbumMsg() // 修改相册信息
		    layui.use('laydate', function(){
				albumPhoto.params.laydate = layui.laydate;
				//执行一个laydate实例
				albumPhoto.params.laydate.render({
				    elem: '#startDate' //指定元素
				});
				//执行一个laydate实例
				albumPhoto.params.laydate.render({
				    elem: '#endDate' //指定元素
				});
				// 创建我的相册
				albumPhoto.createAlbum();
				// 查询用户
				albumPhoto.searchUser();
			});
			// 全部select
			albumPhoto.allselect();
			// 相册类型
			albumPhoto.albumType();
		},
		// 全部select
		allselect:function(){
			$('#status').change(function(){
				if($('#status option:selected').val()=='0'){
					return false
				}else{
					albumPhoto.selAlbumMsg();
				}
			})
		},
		// 相册类型
		albumType:function(){
			$('#group').change(function(){
				if($('#group option:selected').val()=='0'){
					return false
				}else{
					albumPhoto.selAlbumMsg();
				}
			})
		},
		/*用户信息*/
		getUserDetailMsg:function(){
			var sendData={
				userPhone:JSON.parse(sessionStorage.getItem('user')).userPhone,
				role:sessionStorage.getItem('role'),
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},albumPhoto.params.url+"/user/userInfo",function(res){
				if(res.code==0){
					$('#album-count').html('剩余次数'+res.data.count);
				}else{
					alert(res.message);
				}
			})
		},
		/*查看相册*/
		lookalbumPhoto:function(){
			$('#photos li label').unbind('click').click(function(e){
				sessionStorage.setItem('albumId',$(this).attr('id'));
				window.location.href = '/admin/retoucherBackstage/albumPhoto/myTakePhoto/myTakePhoto_detail_page.html'
			})
		},
		// 显示二维码
		showCodeView:function(){
			$('#photos li').mouseover(function(event){
				$(this).find('label').show();
				$(this).find('strong').show();
			})
			$('#photos li').mouseout(function(){
				$(this).find('label').hide();
				$(this).find('strong').hide();
			})
			albumPhoto.lookalbumPhoto();// 查看相册
		},
		/*显示用户相册*/
		showUserAlbum:function(page,limit){
			simple.loading();
			var sendData = {
				eId:JSON.parse(sessionStorage.getItem('user')).userId,
				agentId:JSON.parse(sessionStorage.getItem('user')).agentId,
				page:page,
				limit:limit
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},albumPhoto.params.url+"/photographer/selectAlbum",function(res){
				console.log(res);
				if(res.code==0){
					if(res.data.length !=0){
						albumPhoto.getAlbumMsg(res);
						albumPhoto.pageAlbum(res.count);
					}else{
						simple.alert('暂无数据');
					}
				}else{
					simple.alert('获取相册失败');
				}
				simple.delLoad();
			})
			albumPhoto.searchAlbum(); // 查询相册
		},
		// 修改相册信息
		repairAlbumMsg:function(){
			$('#exampleModal').on('show.bs.modal', function (event) {
				  var button = $(event.relatedTarget) // Button that triggered the modal
				  var recipient = button.data('whatever') // Extract info from data-* attributes
				  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
				  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
				  var modal = $(this)
				  modal.find('.modal-title').text('New message to ' + recipient)
				  modal.find('.modal-body input').val(recipient)
			})
		},
		// 查询相册
		searchAlbum:function(){
			$('.form-inline .search').click(function(){
				albumPhoto.selAlbumMsg();
			})
		},
		// 查询信息
		selAlbumMsg:function(){
			$('#photos').html('');
			$('#page').html('');
			simple.loading()
			var sendData = {
				eId: JSON.parse(sessionStorage.getItem('user')).userId,
				page:'1',
				limit:'10',
				status:$('#status option:selected').val(),
				group:$('#group option:selected').val(),
				agentId:JSON.parse(sessionStorage.getItem('user')).agentId,
				asTime:$('#startDate').val(),
				aeTime:$('#endDate').val(),
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},albumPhoto.params.url+"/user/userAlbum",function(res){
				if(res.code==0){
					if(res.data !=0){
						albumPhoto.getAlbumMsg(res);
						albumPhoto.pageAlbum(res.count) ; // 分页
					}else{
						simple.alert('暂无相册');
					}
				}else{
					alert(res.message);
				}
				simple.delLoad();
			})
		},
		// 获得相册信息
		getAlbumMsg:function(res){
			$.each(res.data,function(i){
				var li="<li><label class='show-code' id="+res.data[i].albumId+" style='display: none;''></label>"+
					"<strong class='photo-scan' style='display: none;''>手机扫码二维码查看</strong>"+
					"<img src="+ res.data[i].cover +" alt='相册'' title='点击查看'' width='400' height='261' />"+
					"<div class='msg'><div class='title'><h2>"+ res.data[i].albumName +"</h2><p class='text-right'>"+res.data[i].createTime+"</p>"+
					"</div><div class='address'><p>"+res.data[i].describe +"</p>"+
					"</div></div></li>"
				$('#photos').append(li);
				// 生成二维码
				$("#"+res.data[i].albumId).qrcode({
					render : "canvas",    //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好
					text :"http://www.atphotos.cn/phone/template"+res.data[i].templateId+".html?albumId="+res.data[i].albumId+"?&",    //扫描了二维码后的内容显示,在这里也可以直接填一个网址，扫描二维码后
					width : "200",               //二维码的宽度
					height : "200",              //二维码的高度
					background : "#ffffff",       //二维码的后景色
					foreground : "#000000",        //二维码的前景色
					src: JSON.parse(sessionStorage.getItem('user')).headPicture             //二维码中间的图片
				});
			})
			albumPhoto.showCodeView() // 显示二维码查看
			albumPhoto.setAlbumMsg() //设置相册信息
		},
		// 分页
		pageAlbum:function(count){
			var page = count/10 < 1 ? 1 : count/10;
			page = parseInt(page);
			$('#page').jqPaginator({
			    totalPages:page,
			    visiblePages: 5,
			    prev: '<li class="prev"><a href="javascript:void(0);"><span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span></a></li>',
			    next: '<li class="next"><a href="javascript:void(0);"><span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span></a></li>',
			    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
			    currentPage: 1,
			    onPageChange: function (num, type) {
			        if(type=='change'){
			        	photo.getPageData(num.toString(),'10');
			        }
			    }
			});
		},
		// 分页查询数据
		getPageData:function(page,limit){
			simple.loading();
			var sendData = {
				eId:JSON.parse(sessionStorage.getItem('user')).userId,
				agentId:JSON.parse(sessionStorage.getItem('user')).agentId,
				page:page,
				limit:limit
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},albumPhoto.params.url+"/photographer/selectAlbum",function(res){
				console.log(res);
				if(res.code==0){
					if(res.data.length !=0){
						albumPhoto.getAlbumMsg(res);
					}else{
						simple.alert('暂无数据');
					}
				}else{
					simple.alert('获取相册失败');
				}
				simple.delLoad();
			})
		},
		// 设置相册信息
		setAlbumMsg:function(){
			$('#photos .address em').click(function(){
				sessionStorage.setItem('albumId',$(this).attr('albumId'));
				$.get('/admin/retoucherBackstage/albumPhoto/editPhoto/editPhoto.html',function(res){
					$('#content').html(res);
				})
			})
		},
		// 创建我的相册
		createAlbum:function(){
			$('#exampleModal').on('shown.bs.modal', function (e) {
				console.log(e);
				albumPhoto.params.laydate.render({
				    elem: '#c-startDate', //指定元素
				    type:'datetime',
				    value:simple.todayDate()
				});
				albumPhoto.params.laydate.render({
				    elem: '#c-endDate', //指定元素
				    type:'datetime',
				    value:simple.todayDate()
				});
			  	$("#city").citySelect({
			    	url:"/script/city.min.js",
			    	prov:"上海",
			    	city:"浦东新区",
					nodata:'none',
					required:true
				});
				// 用户信息
				albumPhoto.getUserDetailMsg();
				// 所属用户
				$('#user').val(JSON.parse(sessionStorage.getItem('user')).userName);
			})
			// 创建信息
			albumPhoto.createAlbumSuccess();

		},
		// 选择所属用户
		searchUser:function(){
			$('#searchUser').click(function(){
				if($('#userPhone').val() ==""){
					simple.alert('请输入手机号');
				}else{
					albumPhoto.getUserMsg();
				}
			})
			document.onkeydown = function(){
				var e = e || window.event;
				if(e.keyCode==13){
					if($('#userPhone').val() ==""){
						simple.alert('请输入手机号');
					}else{
						albumPhoto.getUserMsg();
					}
				}
			}
		},
		// 获得所属用户信息
		getUserMsg:function(res){
			$('.show-msg').html('');
			var sendData = {
				userPhone:$('#userPhone').val(),
				role:'0'
			}
			sendData = JSON.stringify(sendData);
			server.getData( {postData:sendData},albumPhoto.params.url+"/user/userInfo",function(res){
				if(res.code==0){
					var li = "<li title='点击添加' userid="+ res.data.userId +"><span><img src="+res.data.headPicture+" alt='头像'></span><span class='delPeople'>"+ res.data.userName +"</span></li>"
					$('.show-msg').append(li);
				}else{
					simple.alert('没有用户')
				}
				$('.show-msg li').click(function(){
					$('#user').val($(this).find('span').eq(1).html());
					albumPhoto.params.userId = $(this).attr('userid');
					$('#sel-user').modal('hide')
				});
			})
		},
		// 创建相册
		createAlbumSuccess:function(){
			$('.edit-save').click(function(){
				if($('#albumName').val()=='' && $('#albumName').val()=='' && $('#city .prov option:selected').val()==''){
					simple.alert('输入相册名称');
				}else if($('#city .dist option:selected').val()==undefined){
					albumPhoto.params.address = $('#city .prov option:selected').val()+" "+$('#city .city option:selected').val();
				}else if($('#city .dist option:selected').val() !=''){
					albumPhoto.params.address = $('#city .prov option:selected').val()+" "+$('#city .city option:selected').val()+" "+$('#city .dist option:selected').val();
				}
				albumPhoto.params.userId = sessionStorage.getItem('createUserId');
				var pwd = $('#album-pwd').val()=="" ? 'null' : $('#album-pwd').val();
				var editMsg = {
					albumName:$('#albumName').val(),
					asTime:$('#c-startDate').val(),
					aeTime:$('#c-endDate').val(),
					address:albumPhoto.params.address,
					albumTitle:$('#album-title').val(),
					describe:$('#album-msg').val(),
					createUserId:JSON.parse(sessionStorage.getItem('user')).userId,
					passsword:hex_md5(pwd),
					userId:albumPhoto.params.userId,
					startTime:$('#c-album-time').val()
				}
				sendData = JSON.stringify(editMsg);
				server.postData(sendData,albumPhoto.params.url+"/album/createNewAlbum",function(res){
					if(res.code==0){
						simple.alert(res.message);
						$('#exampleModal').modal('hide');
						window.location.reload();
					}else{
						simple.alert(res.message);
					}
				})
			});
		},
	}
	return albumPhoto;
})
